<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Rent_model extends CI_Model { 
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    public function get_car_list(
        $make, 
        $model, 
        $max_price,
        $dealer = '',
        $year = '',
        $location = '',
        $search_type = '%')
    {		
        $extra = "";
        $operator = " AND ";
        if(strlen($make) > 0)
        {
                $extra .= $operator . "make.name = " . $this->db->escape($make);	
        }
        if(strlen($model) > 0)
        {
                $extra .= $operator . "model.name = " . $this->db->escape($model);
        }
        if($max_price > 0)
        {
                $extra .= $operator . "car.price <= " . $this->db->escape($max_price);
        }
		     
        $query_string = 
            "SELECT 
                car.id as id,
                car.price as CarPrice,
                car.quality as quality,
                car.image,
                car.description,
                car.number_votes,
                car.total_points,
                car.dec_avg,
                car.whole_avg, 
                car.door_count,
                car.fuel,                
                car.vehical_type,
                car.drive_train,
                make.name as MakeName,
                model.name as ModelName,    
                car.year as YearCreate,
                type.name as CategoryName,
                ex_color.color as external_color,
                in_color.color as internal_color,
                dealer.name as DealerName,
                dealer.company_number1,
                dealer.email,
                dealer.company,
                location.province,
                location.district,
                service.name as ServiceName,
                service.category as ServiceCategory,
                category.category_name
            FROM    
                psacar_car as car
                -- left join psacar_series as series on car.series_id = series.id 
                left join psacar_model as model on car.model_id = model.id 
                left join psacar_make as make on model.make_id = make.id 
                left join psacar_car_type as type on car.type_id = type.id
                left join psacar_member as dealer on car.dealer_id = dealer.id
                left join psacar_location as location on dealer.location_id = location.id
                left join psacar_color as ex_color on car.external_color_id = ex_color.id
                left join psacar_color as in_color on car.internal_color_id = in_color.id                
                left join psacar_service as service on dealer.service_id = service.id
                left join psacar_category as category on car.category_id = category.category_id
            WHERE
                type.name like '$search_type'" . $extra;              
              
        $result = $this->db->query($query_string);
        return  $result;
    }	
}